define(function(require, exports, module) {
    var plugins = require('plugins');
    J = jQuery;
    require('bootstrap');
	require('bootstraponhover');
    require('tongji');
    require('bootstrap');
    require('hover');
	require('layer');
	layer.config({
	    path: '/assets/vendors/layer/' //layer.js所在的目录，可以是绝对目录，也可以是相对目录
	});
	//用户登录后鼠标经过菜单
	$(".menu-bar").bootstrapDropdownOnHover();
	
	//首页导航条设置
	$(function(){
		$(window).scroll(function(){
	        var height = $("#header").offset().top;
	        //alert(height);
	        if(height > 500){
	   	  	  	$('#header').removeClass('nologin-index');
	   	  	}else{
	   	  	$('#header').addClass('nologin-index');
	   	  	};
		});
	});
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	var showPopover = function () {
        $(this).popover('show');
    }
    , hidePopover = function () {
        $(this).popover('hide');
    };
    $('[data-toggle="tooltip"]').tooltip();
    
	var timenow = new Date().getTime();
	
	var userlogin=0;
	$.ajax({
        type: "get",
        contentType: "application/json",
        url: "/member/ajaxlogin.json?&"+Math.random(),
        data: "{}",
        dataType: 'json',
        async: false, //ajax方法外可以调用变量
        success: function(data) {
        	if(data.code >= 0){
        		userlogin=1;
	        }
        }
    });
	
    var wpexLocalize = {
    		"mobileMenuOpen" : "Click here to navigate",
    		"mobileMenuClosed" : "Close navigation",
    		"isOriginLeft" : "1"
    	};
    
    // 图片懒加载
    var imagesLazyload = function () {
    	require.async('lazyload', function () {
    		$("img").lazyload({
	   	   		 //url地址目前是指定的，正式上线后需修改
    			 placeholder: '',
	   	   		 effect: "fadeIn"
	   	   	});
        });
    };
    
   	if(!navigator.userAgent.match(/(iPhone|iPod|Android|ios|iPad)/i)){
	    $("li.nav-hover").mouseenter(function(event) {
	        $(this).addClass("open");
	    }).mouseleave(function(event) {
	        $(this).removeClass("open");
	    });
	    
	} else {
		$("body").on("click","li.nav-hover",function(){
			$(this).toggleClass("open");
		});

		if ($(".nav-mobile li.nav-hover").is(":has(ul)")) {
			$(".nav-mobile li.nav-hover>a").attr("href","javascript:;");
		}
	}
    
    //搜索框
    $(".js-search").focus(function () {
        $(this).parent().addClass("active");
    }).blur(function(){
        $(this).parent().removeClass("active");
    });
    
    $(".has-unread").on("click", function(){
    	if($(".opts__item--message").hasClass("hide")){
	    	$(".opts__item--message").removeClass("hide");
    	}else{
	    	$(".opts__item--message").addClass("hide");
    	}
    });
    
    $(".navbar-form span").click(function(){
        $("#top-search-form").submit();
    });
    
    // 返回顶部
    var backToTop = function () {
    	var $window = $(window);
    	$scrollTopLink = $( 'a.site-scroll-top' );
		$window.scroll(function () {
			if ($(this).scrollTop() > 100) {
				$scrollTopLink.fadeIn();
			} else {
				$scrollTopLink.fadeOut();
			}
		});		
		$scrollTopLink.on('click', function() {
			$( 'html, body' ).animate({scrollTop:0}, 400);
			return false;
		} );
    };
    
    // 检查登陆状态
	$(".ajaxLogin").on("click", function(){	
		if(userlogin==0){
			$('#myModal').modal('toggle')
		}
	});
    
	//===========================================登录开始=================================================
	//用户登录名
	$("#ajax-login-btn").on("click", function(){
		var username=$("#login_username").val();
		var password=$("#login_password").val();
		//alert(username);
		if(username.length>0){
			if(!/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/.test(username)){
				$("#login_username").closest('div').removeClass('has-success').addClass('has-error');
				$("#login_username_txt").html("<span class=\"text-danger\">用户名必须为邮箱地址</span>");
				$("#login_username").focus();
				return false; 
			}
		}else{
			$("#login_username_txt").html("<span class=\"text-danger\">用户名不能为空</span>");
			$("#login_username").closest('div').removeClass('has-success').addClass('has-error');
			$("#login_username").focus();
			return false; 
		}
		
		if(password.length>=6){
			if(!/^[0-9a-zA-Z_~!@#$%^&*()_+]{6,20}$/.test(password)){
				$("#login_password").closest('div').removeClass('has-success').addClass('has-error');
				$("#login_password_txt").html("<span class=\"text-danger\">密码不能包含特殊字符货空格</span>");
				$("#login_password").focus();
				return false; 
			}
		}else{
			$("#login_password_txt").html("<span class=\"text-danger\">密码必须是6-20位字符</span>");
			$("#login_password").closest('div').removeClass('has-success').addClass('has-error');
			$("#login_password").focus();
			return false;
		}
		//layer.msg("11111", {icon: 5});
		$.ajax({
    		url: '/member/login.json?&'+Math.random(), 
    		data: {'username': username,'password': password,'rememberMe':$("#rememberMe").is(':checked')},
    		dataType: "json",
    		type :  "POST",
    		cache : false,
    		async: false,
    		error : function(i, g, h) {
    			layer.msg('发送错误', {icon: 2});
    		},
    		success: function(data){	
    			if(data.code==0){
    				window.location.reload();
    				return false;
    			}else{
					if(data.message!=''){
						layer.msg(data.message, {icon: 2});
						return false;
					}else if(data.message!=''){
						layer.msg(data.message, {icon: 2});
						return false;
					}
				}
          	}
    	});
		return false;
	});
	$("#login-form").keydown(function(e){
		 var e = e || event,
		 keycode = e.which || e.keyCode;
		 if (keycode==13) {
		  $(".log_btn").trigger("click");
		 }
	});
	//===========================================登陆结束=================================================
	//===========================================注册开始=================================================
	
	//用户注册提交
	$("#register-btn").on("click", function(){
		var username=$("#register_username").val();
		var nickname=$("#register_nickname").val();
		var password=$("#register_password").val();

		if(username.length>0){
			if(!/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/.test(username)){
				$("#register_username").closest('div').removeClass('has-success').addClass('has-error');
				$("#register_username_txt").html("<span class=\"text-danger\">用户名必须为邮箱地址</span>");
				$("#register_username").focus();
				return false; 
			}
		}else{
			$("#register_username_txt").html("<span class=\"text-danger\">用户名不能为空</span>");
			$("#register_username").closest('div').removeClass('has-success').addClass('has-error');
			$("#register_username").focus();
			return false; 
		}
		
		if(nickname.length>=2){
			if(!/^[\u4e00-\u9fa5a-zA-Z0-9_]{2,10}$/.test(nickname)){
				$("#register_nickname").closest('div').removeClass('has-success').addClass('has-error');
				$("#register_nickname_txt").html("<span class=\"text-danger\">昵称不能包含特殊字符货空格</span>");
				$("#register_nickname").focus();
				return false; 
			}
		}else{
			$("#register_nickname_txt").html("<span class=\"text-danger\">昵称必须是2-10位字符</span>");
			$("#register_nickname").closest('div').removeClass('has-success').addClass('has-error');
			$("#register_nickname").focus();
			return false;
		}
		
		if(password.length>=6){
			if(!/^[0-9a-zA-Z_~!@#$%^&*()_+]{6,20}$/.test(password)){
				$("#register_password").closest('div').removeClass('has-success').addClass('has-error');
				$("#register_password_txt").html("<span class=\"text-danger\">密码不能包含特殊字符货空格</span>");
				$("#register_password").focus();
				return false; 
			}
		}else{
			$("#register_password_txt").html("<span class=\"text-danger\">密码必须是6-20位字符</span>");
			$("#register_password").closest('div').removeClass('has-success').addClass('has-error');
			$("#register_password").focus();
			return false;
		}
		
		
		jQuery.ajax({
			type:"POST",
			url: "/member/addUser.json",
			data: {"username":username,"nickname":nickname,"password":password},
			dataType:"json",
			cache : false,
	        async: false, //ajax方法外可以调用变量
	        success : function(data) {
	        	if (data.code==0) {
	        		window.location.href = "/member/me";	
				}else{
					layer.msg(data.message, {icon: 2});
					return false;
				}
	        }
	    });
		return false;
	});
	
	$("#register-btn").keydown(function(e){
		 var e = e || event,
		 keycode = e.which || e.keyCode;
		 if (keycode==13) {
		  $("#register-btn").trigger("click");
		 }
	});
	
	//===========================================注册结束=================================================
	
	
	//===========================================关注开始=================================================
	//用户注册提交
	$(".followerUser").on("click", function(){
		var user_byid=$(this).attr("data-source_id");
		var follow_btn = $(this);
		jQuery.ajax({
			type:"POST",
			url: "/u/userfollow.json",
			data: {"user_byid":user_byid},
			dataType:"json",
			cache : false,
	        async: false, //ajax方法外可以调用变量
	        success : function(data) {
	        	if (data.code==0) {
	        		var datas = eval(data.data);
	        		follow_btn.removeClass('disabled');
	                follow_btn.removeAttr('disabled');
	                if(datas.relation_type < 3){
	                    follow_btn.html('关注');
	                    follow_btn.addClass('active');
	                    layer.msg(data.message, {icon: 1});
	                }else{
	                    follow_btn.html('已关注');
	                    layer.msg(data.message, {icon: 1});
	                    follow_btn.removeClass('active');
	                }
	                if(datas.relation_type == 0){
	                    follow_btn.html('已关注');
	                    follow_btn.addClass('active');
	                    layer.msg(data.message, {icon: 1});
	                }
					return false;
				}else{
					layer.msg(data.message, {icon: 2});
					return false;
				}
	        }
	    });
		return false;
	});
	//===========================================关注结束=================================================
	//加入小组
	$(".followed-btn").on("click", function(){
		var groupid=$(this).attr("data-group-id");
		$.ajax({
    		url: '/member/addgroup.json?'+timenow, 
    		data: {'group_id': groupid},
    		dataType: "json",
    		type :  "POST",
    		cache : false,
    		async: false,
    		error : function(i, g, h) {
    			layer.msg('请登录后操作', {icon: 2});
    		},
    		success: function(data){	
    			if (data.code==0) {
    				layer.msg(data.message, {icon: 1});
    				window.location.reload();
    			}else{
    				layer.msg(data.message, {icon: 2});
    				window.location.reload();
    			}
          	}
    	});
		return false;
	});	
	
	$("#Addlike").on("click", function(){
		var id=$(this).attr("data-source_id");
		$.ajax({
    		url: '/member/topic/addlove.json?'+timenow, 
    		data: {'id': id},
    		dataType: "json",
    		type :  "POST",
    		cache : false,
    		async: false,
    		error : function(i, g, h) {
    			layer.msg('请登录后操作', {icon: 2});
    		},
    		success: function(data){	
    			if (data.code==0) {
    				//alert("Text: " + $("#likecount").text()); 
    				layer.msg(data.message, {icon: 1});
    				window.location.reload();
    			}else{
    				layer.msg(data.message, {icon: 2});
    				window.location.reload();
    			}
          	}
    	});
		return false;
	});	
	
	$(".ajaxtuijian").on("click", function(){
		var id=$(this).attr("data-source_id");
		$.ajax({
    		url: '/member/article/addtuijian.json?'+timenow, 
    		data: {'id': id},
    		dataType: "json",
    		type :  "POST",
    		cache : false,
    		async: false,
    		error : function(i, g, h) {
    			layer.msg('请登录后操作', {icon: 2});
    		},
    		success: function(data){	
    			if (data.code==0) {
    				//alert("Text: " + $("#likecount").text()); 
    				layer.msg(data.message, {icon: 1});
    				window.location.reload();
    			}else{
    				layer.msg(data.message, {icon: 2});
    				window.location.reload();
    			}
          	}
    	});
		return false;
	});	
	
	
	//点击收藏
	$(".AddFavorite").on("click", function(){
		var id=$(this).attr("data-source_id");
		$.ajax({
    		url: '/member/addtopicfavorite.json', 
    		data: {'id': id},
    		dataType: "json",
    		type :  "get",
    		cache : false,
    		async: false,
    		error : function(i, g, h) {
    			layer.msg('发送错误', {icon: 2});
    		},
    		success: function(data){	
    			if(data.code==0){
    				layer.msg(data.message, {icon: 1});
    				window.location.reload();
    			}else{
    				layer.msg(data.message, {icon: 2});
    				window.location.reload();
				}
          	}
    	});
	});
	
	var isshow = 1;
	$(function (){
			$('#check').hover(function (){
				$("#check").stop().animate({
					height:'170px'
				},500);
				$('#checkdetail').show();
				$('#checkdetail').stop().animate({
					opacity: 1
				},500);
		},function (){
				$("#check").stop().animate({
					height:'70px'
				});
				$('#checkdetail').stop().animate({
					opacity: 0
				},500);
		});
		});
	function checkin(){
		//未登录
		if( MID == 0 ){
			ui.quicklogin();
			return;
		}
		var credit_score = "5";
		var totalnum = 1 + 1;
		$.post(U('widget/CheckIn/check_in') , {} , function (res){
			if(res){
				$('#checkin').text('已签到');
				$('#checkin').attr('class' , 'btn-sign-h');
				$('#checkdiv').attr('class' , 'sign-in-h' );
				$('#checkinfo').text('签到成功，获取积分' + credit_score + '分');
				
				var connum = res;
				$('#con_num').text(connum);
				$('#con_num_day').text(connum);
				$('#total_num').text(totalnum);
				$('#check').hover(function (){
						$("#check").stop().animate({
							height:'170px'},500);
						$('#checkdetail').show();
						$('#checkdetail').stop().animate({
							opacity: 1
						},500);
				},function (){
						$("#check").stop().animate({
							height:'70px'
						},500);
						$('#checkdetail').stop().animate({
							opacity: 0
						},500);
				});
			}
		});
	}
    
});